define(function(require, exports, module) {
  'use strict';

  var $ = require('$');

  var Form = require('../../common/form/form');
  var Tags = require('../../common/tags/tags');
  var Select = require('../../common/select/select');
  var io = require('../../common/io');
  var Similar = require('../similar/similar');

  module.exports = Form.extend({
    defaults: {
      container: '#J_Form',
      isEdit: false,
      url: 'kmInfoSave',
      customRules: {
        docSubject: {
          async: function(callback, el) {
            var self = this;
            var params = {
              templateName: el.value.trim()
            };
            if (self.option('isEdit')) {
              params.id = self.option('id');
            }
            io.post('kmInfoExistTitle', params, function() {
              callback.call(self, !this.data);
            });
          }
        }
      },
      customMessages: {
        docSubject: {
          async: '标题名称已存在，请重输！'
        }
      },
      delegates: {
        'change [data-role=title]': function(e) {
          this.similar.setQuestion($(e.currentTarget).val());
        }
      },
      events: {
        done: function() {
          window.location.href = '/admin/knowledge/info.html';
        },
        render: function() {
          var self = this;
          new Select({
            url: 'categoryList',
            hasOptionAll: false,
            multiple: true,
            key: {
              text: 'docSubject'
            },
            field: $('[name=cateIds]')
          });

          this.similar = new Similar({
            field: $('#similar')
          });
        }
      }

    }
  });
});
